package com.szxy.miaosha.utils;

import com.szxy.miaosha.domain.MiaoshaUser;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Date;

/**
 * @Auther:wzer
 * @Date:2020/9/6
 * @Description:com.szxy.miaosha.utils
 * @Version:1.0 passform = md5(inputPass, slat);
 * passDb = md5(passform, random_slat)
 **/
public class Md5Util {

    /**
     * 客户端盐值 salt
     */
    public static final String CLIENT_SALT = "1a2b3c4";

    /**
     * MD5 加密
     *
     * @param pass 密文
     * @return
     */
    public static String md5(String pass) {
        return DigestUtils.md5Hex(pass);
    }

    /**
     * 客户端 md5 加密， 固定盐值
     * @param pass 密文
     * @return
     */
    public static String inputPassToFormPass(String pass) {
        String formPass = "" + CLIENT_SALT.charAt(0) + CLIENT_SALT.charAt(2) + pass
                + CLIENT_SALT.charAt(4) + CLIENT_SALT.charAt(6);
        return md5(formPass);
    }

    /**
     * 服务器端 md5 加密， 随机盐值
     * @param pass 密文
     * @param salt 随机盐值
     * @return
     */
    public static String inputPassToDbPass(String pass, String salt) {
        String dbPass = salt.charAt(0) + salt.charAt(2) + pass
                + salt.charAt(4) + salt.charAt(6);
        return md5(dbPass);
    }


    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        MiaoshaUser user = new MiaoshaUser();
        user.setId(13000000000L);
        user.setLoginCount(1);
        user.setNickname("user");
        user.setRegisterDate(new Date());
        user.setSalt("1a2b3c4");
        user.setPassword(Md5Util.inputPassToDbPass("123456", user.getSalt()));
        System.out.println(user);
    }

}
